# -*- coding: utf8 -*-

import os
import jinja2
import webapp2

from statistics.word_statistic import *
from environment import *


class HTMLStatParams():
    pass


class ShowWordStatistic(webapp2.RequestHandler):

    def get_stat(self, hist_ttime, hist_ltime_nattempt):
        html_params = HTMLStatParams()
        html_params.total_guess = 0
        html_params.total_fail = 0
        histogram_ttime = []
        histogram_nattempt = []
        histogram_ttime_head = u'[\'Время\', \'Отгадано\', \'Фолов\']'
        histogram_nattempt_head = u'[\'Количество попыток\',\
            \'Отгадано\', \'Фолов\']'
        total_time_sec = 0
        total_games = 0
        for stat in hist_ttime:
            html_params.total_guess += stat.cnt_guess
            html_params.total_fail += stat.cnt_fail
            total_time_sec += stat.time_sec * (stat.cnt_guess +
                                               stat.cnt_fail)
            total_games += stat.cnt_guess + stat.cnt_fail
            if stat.time_sec < 120:
                found = False
                for stat_exists in histogram_ttime:
                    if stat_exists[0] == stat.time_sec:
                        stat_exists[1] += stat.cnt_guess
                        stat_exists[2] += stat.cnt_fail
                        found = True
                if not found:
                    histogram_ttime.append([stat.time_sec, stat.cnt_guess,
                                            stat.cnt_fail])
        for stat in hist_ltime_nattempt:
            if stat.num_attempts <= 10:
                found = False
                for stat_exists in histogram_nattempt:
                    if stat_exists[0] == stat.num_attempts:
                        stat_exists[1] += stat.cnt_guess
                        stat_exists[2] += stat.cnt_fail
                        found = True
                if not found:
                    histogram_nattempt.append([stat.num_attempts,
                                               stat.cnt_guess,
                                               stat.cnt_fail])
        if total_games == 0:
            return None
        html_params.time_average_sec = (float(total_time_sec) /
                                        total_games)
        html_params.guess_percent = (html_params.total_guess *
                                     100.0 / total_games)
        html_params.fail_percent = (html_params.total_fail *
                                    100.0 / total_games)
        html_params.total_games = total_games
        html_params.histogram_ttime = '[' + histogram_ttime_head + ', '
        for elem in histogram_ttime:
            html_params.histogram_ttime += str(elem) + ', '
        html_params.histogram_ttime += ']'
        html_params.histogram_nattempt = (
            '[' + histogram_nattempt_head + ', ')
        for elem in histogram_nattempt:
            html_params.histogram_nattempt += str(elem) + ', '
        html_params.histogram_nattempt += ']'
        return html_params

    def get(self):
        word_text = self.request.get('word')
        word = WordStatistic.get_by_word(word_text)
        html_params = {
            'word_text': word_text,
            'word_exists': False
        }
        if word is not None:
            html_params['word_exists'] = True
            html_params['hat_standart'] = self.get_stat(
                word.hist_ttime, word.hist_ltime_nattempt)
            html_params['crocodile'] = self.get_stat(word.croc_ttime,
                                                     word.croc_ltime_nattempt)
        template = ENVIRONMENT.get_template('word_statistic.html')
        self.response.write(template.render(html_params))
